package com.zhuss.core.processor;

import java.util.*;

import com.zhuss.bean.unit.core.user.UserBean;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.diss.CreateDissBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.DissDao;
import com.zhuss.core.db.entity.Diss;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;


/**
 * @description: 吐槽创建
 * @author: zhushanshuo
 * @create: Tue Feb 02 15:22:08 CST 2021
 **/
@Component("dissCreate")
public class DissCreate extends AbstractProcessor<Object, CreateDissBo> implements Processor<Object, CreateDissBo> {

    @Autowired
    private DissDao dissDao;

    @Autowired
	@Qualifier("dissRead")
	private DissRead dissRead;


    /**
     * 校验
     */
    @Override
    public void check(CreateDissBo bo) throws DzcxException {
    	String name = bo.getTitle();
    	if (name == null || "".equals(name.trim())) {
    		 throw new DzcxException(ExceptionCode.意见反馈标题为空);
    	}

    	if (bo.getContent() == null || "".equals(bo.getContent().trim())) {
			throw new DzcxException(ExceptionCode.意见反馈内容为空);
		}

//    	List<Diss> list = dissRead.list(name);
//    	if (!list.isEmpty()) {
//    		 throw new DzcxException(ExceptionCode.建议意见标题为空);
//    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(CreateDissBo param) {
    	Diss record = new Diss();
		UserBean user =  param.getConcurrentUser();
    	record.setTitle(user.getPhone() == null ? param.getTitle() : user.getPhone());
    	record.setContent(param.getContent());
    	record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());


    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	dissDao.create(record);
    	param.setTargetId(record.getDissId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(CreateDissBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.意见反馈创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getTitle());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
